package com.yingxin.yxerp.module.inform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yingxin.yx.framework.commons.dto.PageOut;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.commons.id.IdHelper;
import com.yingxin.yx.framework.mybatisplus.PageHelper;
import com.yingxin.yxerp.module.inform.entity.NoticeType;
import com.yingxin.yxerp.module.inform.entity.builder.NoticeTypeBuilder;
import com.yingxin.yxerp.module.inform.service.INoticeTypeService;
import com.yingxin.yxerp.module.inform.vo.req.NoticeTypeCreateVo;
import com.yingxin.yxerp.module.inform.vo.req.NoticeTypeQueryVo;
import com.yingxin.yxerp.module.inform.vo.req.NoticeTypeUpdateVo;
import com.yingxin.yxerp.module.inform.vo.resp.NoticeTypeRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 通知公告-公告类型 前端控制器
 * @author zhuchangjie
 * @since 2021-12-21
 */

@RestController
@RequestMapping("/inform/notice-type")
public class NoticeTypeController {

    @Autowired
    private INoticeTypeService noticeTypeService;


    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public PageOut<NoticeTypeRespVo> page(@RequestBody NoticeTypeQueryVo queryVo) {
        IPage<NoticeTypeRespVo> page = noticeTypeService.pageVo(PageHelper.in(queryVo), queryVo);
        return PageHelper.out(page);
    }


    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<NoticeTypeRespVo> list(@RequestBody NoticeTypeQueryVo queryVo) {
        return noticeTypeService.listVo(queryVo);
    }


    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public NoticeTypeRespVo get(@PathVariable("id") String id) {
        return noticeTypeService.getVo(id);
    }

    @PreAuthorize("hasAuthority('notice:type:create')")
    @ApiOperation(value = "新增")
    @PostMapping
    public String create(@RequestBody @Valid NoticeTypeCreateVo createVo) {
        Integer count = noticeTypeService.countByTypeNameNeId(createVo.getTypeName(), null);
        if (count > 0) {
            throw new BizException("类型已存在");
        }
        NoticeType noticeType = NoticeTypeBuilder.build(createVo);
        noticeType.setId(IdHelper.snowflakeId());
        noticeTypeService.create(noticeType);
        return noticeType.getId();
    }

    @PreAuthorize("hasAuthority('notice:type:update')")
    @ApiOperation(value = "修改")
    @PutMapping
    public String update(@RequestBody @Valid NoticeTypeUpdateVo updateVo) {
        Integer count = noticeTypeService.countByTypeNameNeId(updateVo.getTypeName(), updateVo.getId());
        if (count > 0) {
            throw new BizException("类型已存在");
        }
        NoticeType noticeType = NoticeTypeBuilder.build(updateVo);
        noticeTypeService.update(noticeType);
        return noticeType.getId();
    }

    @PreAuthorize("hasAuthority('notice:type:delete')")
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        NoticeType noticeType = noticeTypeService.getById(id);
        noticeTypeService.delete(noticeType);
    }

}
